package com.enbatis.mybatisplugs.controller;

import com.enbatis.mybatisplugs.base.BaseController;
import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.commons.utils.ResultReturn;
import com.enbatis.mybatisplugs.plugin.Pages;
import com.github.pagehelper.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.enbatis.mybatisplugs.entity.LeaveApply;
import com.enbatis.mybatisplugs.service.LeaveService;


import java.util.List;

@RestController
@RequestMapping("v1/leave")
public class LeaveController extends BaseController {

@Autowired
private LeaveService leaveService;


    @PostMapping("/add")
    public ResultReturn<LeaveApply> add(@RequestBody LeaveApply leave){
        return  success(leaveService.insert(leave));
    }


    @DeleteMapping("/delete")
    public ResultReturn<Integer> delete(@RequestParam("id") Long id){
        return success(leaveService.deleteById(id));
    }


    @PutMapping("/{id}")
    public  ResultReturn<Integer> update(@PathVariable("id") Long id,@RequestBody LeaveApply leave){
        leave.setId(id);
        return success(leaveService.updateById(leave));
    }


    @GetMapping("/{id}")
    public ResultReturn<LeaveApply> get(@PathVariable("id") Long id) {
        return success(leaveService.getById(id));
    }


    @GetMapping("/list")
    public ResultReturn<List<LeaveApply>> getList(LeaveApply leave){
        return success(leaveService.list(new Wrapper<>(leave)));
    }



    @PostMapping("/add_draft")
    public ResultReturn addDraft(@RequestBody LeaveApply leave){
        return  success(leaveService.addDraft(leave,getAccount()));
    }


    @PostMapping("/add_submit")
    public ResultReturn addSubmit(@RequestBody LeaveApply leave){
        return  success(leaveService.addSubmit(leave,getAccount()));
    }


    @PostMapping("/submit_process")
    public ResultReturn submitProcess(@RequestParam("ids") Long[] ids){
        return  success(leaveService.submitProcess(ids,getAccount()));
    }



    @PostMapping("/pass")
    public ResultReturn pass(@RequestParam("taskId") String taskId,@RequestParam("businessId") Long businessId,@RequestParam("opinion") String opinion){
        return  success(leaveService.pass(taskId,businessId,opinion,getAccount()));
    }


    @PostMapping("/back")
    public ResultReturn back(@RequestParam("taskId") String taskId,@RequestParam("businessId") Long businessId,@RequestParam("opinion") String opinion){
        return  success(leaveService.back(taskId,businessId,opinion,getAccount()));
    }


    @ApiOperation(value = "分页列表", notes = "分页查询数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求数据的页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "rows", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sord", value = "排序方式", paramType = "query", dataType = "String"),
    })
    @PostMapping("/page")
    public ResultReturn<Pages<LeaveApply>> getPage(@RequestBody LeaveApply category){
        Page<LeaveApply> page= getPage();
        return success(leaveService.page(page,new Wrapper<>(category)));
    }

}
